/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prueba.rutas;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.ConcurrentModificationException;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 *
 * @author Marcy
 */
public class Mapa extends JPanel implements Runnable{
    
    public static int FW, FH;
    Thread animador;
    Image imagen;
    Calendar calendario;
    Label mostrarHora;
    int hora;
    ArrayList<Vehiculo> trafico;
    Timer admin;
    int ancho=21;
    int alto=16;
    public Mapa(){
       // this.setSize(500,500);
        setPreferredSize(new Dimension(650,500));
        admin = new Timer(1000, ponerAuto);
        int fw = 650/ancho;
        int fh = 500/alto;
        FW=fw;
        FH=fh;
        trafico = new ArrayList<>();
        admin.start();
        animador = new Thread(this);
        imagen=Toolkit.getDefaultToolkit().getImage(getClass().getResource("fondo.png"));
      
    }
    @Override
    public void paint(Graphics g){
        Graphics2D pincel = (Graphics2D)g;
        Graphics2D g2=(Graphics2D)g;
        dibujarMapa(pincel);
        for (Vehiculo v : trafico) {
            v.dibujarme(g2);
            if(!v.estado())
                trafico.remove(v);
        }
    }
    private void dibujarMapa(Graphics2D pincel){
//     System.out.println(calendario.get(Calendar.HOUR_OF_DAY)+" : "+calendario.get(Calendar.MINUTE));
    
        pincel.setColor(Color.gray);
        pincel.fillRect(0, 0, getWidth(), getHeight());
               
        int fw = getWidth()/ancho;
        int fh = getHeight()/alto;
        Mapa.FW=fw;
        Mapa.FH=fh;
   
        pincel.setColor(Color.gray);
        pincel.setFont(new Font("Arial", Font.BOLD, 64));
        pincel.drawString(Calendar.HOUR_OF_DAY+ " : "+ Calendar.MINUTE+" : "+Calendar.SECOND, 100, 50);
        pincel.setColor(Color.white);
        pincel.drawLine( 0*fw,9*fh+(7*fh/10),20*fw,10*fh+fh/2);
        pincel.drawLine( 7*fw+fw/2+fw/8,17*fh, 10*fw,11*fh+fh/8);
        pincel.setColor(Color.blue);
        
        //poligono 1
        Polygon polygon= new Polygon();
        polygon.addPoint(0,0);
        polygon.addPoint(0, 9*fh+(fh/5));
        polygon.addPoint(2*fw+(fw/5), 9*fh+(fh/4));
        polygon.addPoint(2*fw,0);
        pincel.fillPolygon(polygon);
        
        Polygon polygon2= new Polygon();
        polygon2.addPoint(3*fw,(2*fh)+(fh/3));
        polygon2.addPoint(3*fw+(fw/3), 9*fh+fh/4);
        polygon2.addPoint((14*fw)+(fw/3), 9*fh+fh/2);
        polygon2.addPoint(14*fw+fw/6, 3*fh+(2*(fh/3)));
        pincel.fillPolygon(polygon2);
        
        Polygon polygon3= new Polygon();
        polygon3.addPoint(15*fw+fw/4, 0);
        polygon3.addPoint(15*fw+fw/2, 9*fh+fh/4);
        polygon3.addPoint(19*fw+fw/3,9*fh+fh/5);
        polygon3.addPoint(20*fw, 0);
        pincel.fillPolygon(polygon3);
         
        Polygon polygon4= new Polygon();
        polygon4.addPoint(0, 10*fh+fh/5);
        polygon4.addPoint(0, 16*fh);
        polygon4.addPoint(fw/2, 16*fh);
        polygon4.addPoint(3*fw+fw/4,10*fh +fh/3);
        pincel.fillPolygon(polygon4);
     
        Polygon polygon5= new Polygon();
        polygon5.addPoint(4*fw+(8*(fw/10)),(10*fh)+(fh/3));
        polygon5.addPoint(fw, 17*fh);
        polygon5.addPoint(6*fw+(fw/2), 17*fh);
        polygon5.addPoint(8*fw+fw/3, 10*fh+fh/2);
        pincel.fillPolygon(polygon5);
        
        Polygon polygon6= new Polygon();
        polygon6.addPoint(11*fw+fw/5, 11*fh);
        polygon6.addPoint(9*fw,16*fh+(fh/2));
        polygon6.addPoint(14*fw+fw/2, 16*fh+(fh/2));
        polygon6.addPoint(16*fw+fw/3, (11*fh)+(fh/2));
        pincel.fillPolygon(polygon6);

        Polygon polygon7= new Polygon();
        polygon7.addPoint(17*fw+(fw/5),11*fh+fh/2);
        polygon7.addPoint(15*fw,16*fh+fh/2);
        polygon7.addPoint(20*fw, 16*fh+(fh/2));
        polygon7.addPoint(19*fw,11*fh+(2*(fh/3))); 
        pincel.fillPolygon(polygon7);
        
        Polygon polygon8= new Polygon();
        polygon8.addPoint(23*fw+2*(fw/5),1*fh+fh/3);
        polygon8.addPoint(22*fw+fw/5,7*fh+fh/2);
        polygon8.addPoint(24*fw, 7*fh+(fh/2));
        polygon8.addPoint(24*fw,3*fh+(2*(fh/3))); 
        pincel.fillPolygon(polygon8);
        
        Polygon polygon9= new Polygon();
        polygon9.addPoint(24*fw+fw/5,7*fh+fh/2);
        polygon9.addPoint(22*fw+fw/5,7*fh+fh/2);
        polygon9.addPoint(22*fw,10*fh);
        polygon9.addPoint(24*fw+fw/5, 20*fh+(fh/2));
        pincel.fillPolygon(polygon9);
        
        Polygon polygon10= new Polygon();
        polygon10.addPoint(22*fw,10*fh);
        polygon10.addPoint(22*fw,13*fh);
        polygon10.addPoint(23*fw+fw/5,17*fh+fh/2);
        polygon10.addPoint(24*fw+fw/5, 20*fh+(fh/2));
        pincel.fillPolygon(polygon10);

        
        
    }
    private void actualizarInformacion(){
        try{
               for (Vehiculo v : trafico) {
                        v.conducir();
                   }
            }catch(ConcurrentModificationException cme){
                JOptionPane.showMessageDialog(null, cme.getMessage() );
            }
    }
    @Override
    public void run() {
        while(true){
            actualizarInformacion();
            //esperar
            try{
                    Thread.sleep(20);
            }catch(RuntimeException | InterruptedException rt){
                System.exit(0);
            }
            repaint();
        }
    }
  
    ActionListener ponerAuto = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try{
                trafico.add(new Vehiculo(trafico));
            }catch(ConcurrentModificationException cme){
                JOptionPane.showMessageDialog(null, cme.getMessage() );
            }
        }
    };
}
